package com.holyn.guofang.view.distinguish;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.model.constant.AlgorithmConstant;
import com.holyn.guofang.model.webhttp.wo.AlgorismWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.brand.BrandContract;
import com.holyn.guofang.presenter.brand.BrandPresenter;
import com.holyn.guofang.view.BaseFragment;
import com.luck.picture.lib.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Holyn on 2017/7/21.
 */

public class AlgorithmFragment extends BaseFragment implements BaseView, BrandContract.AlgorismWoView{
    private static final int REQUEST_ACTION_POST_ALGORITHM = 0x0001;

    public static final String BUNDLE_KEY_TYPE_CODE = "BUNDLE_KEY_TYPE_CODE";

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.rb_select_same)
    RadioButton rbSelectSame;
    @BindView(R.id.rb_select_similar)
    RadioButton rbSelectSimilar;
    @BindView(R.id.rb_select_default)
    RadioButton rbSelectDefault;
    @BindView(R.id.rg_select)
    RadioGroup rgSelect;

    String typeCode;

    private BrandContract.Presenter presenter;

    private List<AlgorismWo.TypeBean> itemList = new ArrayList<>();
    private List<Boolean> isCheckList = new ArrayList<>();
    private MyRecyclerAdapter recyclerAdapter;

    public static AlgorithmFragment newInstance(String typeCode) {
        Bundle args = new Bundle();
        args.putString(BUNDLE_KEY_TYPE_CODE, typeCode);
        AlgorithmFragment fragment = new AlgorithmFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_algorithm;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        typeCode = getArguments().getString(BUNDLE_KEY_TYPE_CODE);

        presenter = new BrandPresenter(this);
        presenter.addAlgorismWoView(this);

        recyclerAdapter = new MyRecyclerAdapter(itemList, isCheckList);

        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        rvList.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
        rvList.setAdapter(recyclerAdapter);

        rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb_select_same:
                        AlgorithmConstant.setDefaultIsCheckList(typeCode, isCheckList, itemList);
                        break;
                    case R.id.rb_select_similar:
                        for (int i = 0; i<itemList.size(); i++){
                            isCheckList.set(i, true);
                        }
                        break;
                    case R.id.rb_select_default:
                        AlgorithmConstant.setDefaultIsCheckList(typeCode, isCheckList, itemList);
                        break;
                }
                recyclerAdapter.notifyDataSetChanged();
            }
        });

        presenter.postAlgorism(REQUEST_ACTION_POST_ALGORITHM, typeCode);
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(int requestAction, AlgorismWo algorismWo) {
        this.itemList.clear();
        this.itemList.addAll(algorismWo.getType());
        this.isCheckList.clear();
        for (int i = 0; i < algorismWo.getType().size(); i++){
            this.isCheckList.add(false);
        }
        AlgorithmConstant.setDefaultIsCheckList(typeCode, isCheckList, itemList);
        recyclerAdapter.notifyDataSetChanged();
    }

    /**
     *
     * @return
     */
    public List<AlgorismWo.TypeBean> getSelect(){
        List<AlgorismWo.TypeBean> typeBeanList = new ArrayList<>();
        for (int i = 0; i < isCheckList.size(); i++){
            boolean isCheck = isCheckList.get(i);
            if (isCheck){
                typeBeanList.add(itemList.get(i));
            }
        }
        return typeBeanList;
    }

    public String getTypeCode(){
        return typeCode;
    }

    private class MyRecyclerAdapter extends BaseQuickAdapter<AlgorismWo.TypeBean, BaseViewHolder> {
        private boolean onBind;
        private List<Boolean> isCheckList;
        public MyRecyclerAdapter(@Nullable List<AlgorismWo.TypeBean> data, List<Boolean> isCheckList) {
            super(R.layout.check_custom, data);
            this.isCheckList = isCheckList;
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, AlgorismWo.TypeBean item) {
            onBind = true;
            CheckBox cbCustom = baseViewHolder.getView(R.id.cb_custom);
            cbCustom.setText(item.getName());
            cbCustom.setChecked(isCheckList.get(baseViewHolder.getAdapterPosition()));
            onBind =false;
            cbCustom.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!onBind){
                        isCheckList.set(baseViewHolder.getAdapterPosition(), isChecked);
                        notifyDataSetChanged();
                    }
                }
            });
        }
    }
}
